//
//  PCModifyGenderController.m
//  PhotoCircle
//
//  Created by GL on 15/12/18.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCModifyGenderController.h"
#import "PCUserModel.h"
#import "PCMeStore.h"
#import "GMString.h"
#import "GMTipsHelper.h"
#import "photoBtnDelegate.h"
#import "photoBtnView.h"

@interface PCModifyGenderController ()<UITextFieldDelegate,UIActionSheetDelegate,photoBtnDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, strong) PCUserModel *userModel;
@property (nonatomic, strong) photoBtnView *photoButtonView;

@end

@implementation PCModifyGenderController

-(PCUserModel *)userModel{
    _userModel = [[PCUserInfoManager sharedUserInfo] readUserInfo];
    return _userModel;
}

-(void)configUI{
    _textField.text = self.userModel.userGender;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)showGenderSelector
{
    UINib *nib = [UINib nibWithNibName:@"photoBtnView" bundle:[NSBundle mainBundle]];
    _photoButtonView = [[nib instantiateWithOwner:nil options:nil] firstObject];
    _photoButtonView.photoBtnDelegate = self;
    [_photoButtonView setFisrtTitle:@"男" secondTitle:@"女"];
    [self.navigationController.view addSubview:_photoButtonView];
    
}

-(void)photographUpload
{
    _textField.text = @"男";
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
}

-(void)albumUpload
{
    _textField.text = @"女";
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
}

-(void)cancelAction
{
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        _textField.text = @"男";
    }else if (buttonIndex == 1){
        _textField.text = @"女";
    }
}

#pragma mark --
#pragma mark -- UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    [self showGenderSelector];
}

#pragma mark -
#pragma mark -ButtonEvent
- (IBAction)backEvent:(UIButton *)sender {
    [GMTipsHelper showDismiss];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)modifyGender:(UIButton *)sender {
    //修改性别
    if ([NSString isEmptyString:_textField.text]) {
        return;
    }
    
    NSString *gender = [self markGender];
    NSDictionary *pDic = @{@"attr_name":@"gender",@"attr_val":gender,@"token":self.userModel.userToken};
    [GMTipsHelper showLoading];
    [[PCMeStore sharedMeStore] modifyUserName:pDic successHandler:^(NSInteger responCode){
        if (responCode == API_RESPONSE_OK) {
            [GMTipsHelper showSuccess:@"修改成功"];
            self.userModel.userGender = _textField.text;
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:self.userModel];
            [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.8];
        }else{
            [GMTipsHelper showFail:@"修改失败"];
        }
    } errorHandler:^{
        [GMTipsHelper showBadNetwork];
    }];
}

-(void)popViewController
{
    [self backEvent:nil];
}

-(NSString*)markGender{
    
    if ([_textField.text isEqual:@"男"]) {
        return @"m";
    }else{
        return @"f";
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
